package com.cn.view;


import com.cn.controller.Select;
import com.cn.controller.Updata;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class GoodsADD extends JFrame {
    private JTextField id,name,num,price,img;
    private JButton button;
    private JButton button_1;

    public GoodsADD() {
        super("商品管理系统");
        this.setBounds(0, 0, 400, 450);
        this.setLocationRelativeTo(null);//让窗口在屏幕中间显示
        this.setResizable(false);//让窗口大小不可改变
        getContentPane().setLayout(null);

        JLabel label = new JLabel("商品编号：");
        label.setBounds(85, 89, 87, 22);
        getContentPane().add(label);

        id = new JTextField();
        id.setBounds(147, 90, 142, 21);
        getContentPane().add(id);
        id.setColumns(10);

        JLabel label_1 = new JLabel("商品名称");
        label_1.setBounds(85, 139, 87, 22);
        getContentPane().add(label_1);

        name = new JTextField();
        name.setColumns(10);
        name.setBounds(147, 140, 142, 21);
        getContentPane().add(name);

        JLabel label_2 = new JLabel("商品库存：");
        label_2.setBounds(85, 193, 87, 22);
        getContentPane().add(label_2);

        num = new JTextField();
        num.setColumns(10);
        num.setBounds(147, 194, 142, 21);
        getContentPane().add(num);

        JLabel label_3 = new JLabel("商品单价：");
        label_3.setBounds(85, 241, 87, 22);
        getContentPane().add(label_3);

        price = new JTextField();
        price.setColumns(10);
        price.setBounds(147, 242, 142, 21);
        getContentPane().add(price);

        JLabel label_4 = new JLabel("商品照片：");
        label_4.setBounds(85, 290, 87, 22);
        getContentPane().add(label_4);

        img = new JTextField();
        img.setColumns(10);
        img.setBounds(147, 291, 142, 21);
        getContentPane().add(img);

        button = new JButton("确定");
        button.setBounds(78, 317, 93, 23);
        getContentPane().add(button);
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                String addId = id.getText();
                String addName = name.getText();
                String addNum = num.getText();
                String addPrice = price.getText();
                String addImg = img.getText();
                if (addName.equals("")||addName.equals("")||addNum.equals("")||addPrice.equals("")) {
                    JOptionPane.showMessageDialog(null, "请完整输入要添加的数据");
                } else {
                    String sql="INSERT INTO goods_goods VALUES("+addId+",'"+addName+"','"+addPrice+"','"+addNum+"','"+addImg+"')";
                    int result = Updata.addData(sql);
                    if (result>0) {
                        JOptionPane.showMessageDialog(null, "添加成功！");
                        JOptionPane.showMessageDialog(null, "记得刷新一下哦！");
                        dispose();
//						GoodsManage i = new GoodsManage();
//						i.setVisible(true);
                    } else {
                        JOptionPane.showMessageDialog(null, "添加失败！");
                    }
                }

            }
        });

        button_1 = new JButton("取消");
        button_1.setBounds(208, 317, 93, 23);
        getContentPane().add(button_1);
        button_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                dispose();
            }
        });

    }

}
